﻿using Coldairarrow.Util;
using NetTopologySuite.Index.Bintree;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
using static Npgsql.PostgresTypes.PostgresCompositeType;

namespace Coldairarrow.Business.Interface
{
    /// <summary>
    /// 根据主键判断目标数据需要更行的部分
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public class ComparerFromKeys<T> : IEqualityComparer<T>
    {
        /// <summary>
        /// 需要比较的过滤键
        /// </summary>
        public List<ComparerKeys> _keys;

        /// <summary>
        /// 需要更新的行字段名称
        /// </summary>
        public string _rowId;

        /// <summary>
        /// 根据主键判断目标数据需要更新的部分
        /// </summary>
        /// <param name="keys"></param>
        /// <param name="rowId"></param>
        public ComparerFromKeys(List<ComparerKeys> keys, string rowId)
        {
            _keys = keys;
            _rowId = rowId;
        }

        public bool Equals(T? x, T? y)
        {
            bool flag = false;
            string v1 = "";
            string v2 = "";
            
            if (ReferenceEquals(x, y))
                return true;
            if (x is null || y is null)
                return false;
            //根据传入的keys循环判断两个对象是否有不同的部分，如有则返回true
            foreach (var v in _keys)
            {
                v1 = x.GetPropertyValue(v.Field).IsNullOrEmpty() ? "" : x.GetPropertyValue(v.Field).ToString();
                v2 = y.GetPropertyValue(v.Field).IsNullOrEmpty() ? "" : y.GetPropertyValue(v.Field).ToString();
                decimal decTemp1;
                decimal decTemp2;
                DateTime dateTemp1;
                DateTime dateTemp2;
                //是否是数字
                if (decimal.TryParse(v1, out decTemp1)&& decimal.TryParse(v2, out decTemp2))
                {
                    //浮点数向下取整来判断
                    flag = decTemp1.ToString("F6") != decTemp2.ToString("F6") ? true : false;
                    if (flag && y.GetPropertyValue(_rowId).IsNullOrEmpty())
                    {
                        y.SetPropertyValue(_rowId, x.GetPropertyValue(_rowId));
                        break;
                    }
                }
                //是否是日期
                else if (DateTime.TryParse(v1, out dateTemp1) && DateTime.TryParse(v2, out dateTemp2))
                {
                    //浮点数向下取整来判断
                    flag = dateTemp1.ToString("yyyy-MM-dd") != dateTemp2.ToString("yyyy-MM-dd") ? true : false;
                    if (flag && y.GetPropertyValue(_rowId).IsNullOrEmpty())
                    {
                        y.SetPropertyValue(_rowId, x.GetPropertyValue(_rowId));
                        break;
                    }
                }
               //都不是则转换成字符串
                else
                {
                    //如果又不一样的值则返回true需要更新
                    flag = v1.Trim() != v2.Trim() ? true : false;
                    if (flag && y.GetPropertyValue(_rowId).IsNullOrEmpty())
                    {
                        y.SetPropertyValue(_rowId, x.GetPropertyValue(_rowId));
                        break;
                    }
                }
            }
            return flag;
        }

        public int GetHashCode([DisallowNull] T obj)
        {

            ComparerKeys key = _keys.Where(x => x.Type == "K").First();
            return obj.GetPropertyValue(key.Field).ToString().GetHashCode();
        }
    }
}
